#!/bin/bash

# === JAVA configuration
# ======================================================================

[ -n "$CP" ]     || CP=../build/classes
[ -n "$OUTPUT" ] || OUTPUT=../results/$(date +"%Y%m%d-%H%M")-
function cmd {
	echo java -classpath $CP edu.uoc.sd.fr14.Main "$@" 2>&1 | tee $OUTPUT-$NODE.txt
	java -classpath $CP edu.uoc.sd.fr14.Main "$@" 2>&1 | tee -a $OUTPUT-$NODE.txt &
}


# === Script default configuration
# ======================================================================

[ -n "$PORT"      ] || PORT=43211       # default port
[ -n "$SERVERS"   ] || SERVERS=3        # default server count
[ -n "$DEBUG"     ] || DEBUG=           # no debug by default, non empty if debug
[ -n "$START"     ] || START=           # start locals as many servers
[ -n "$LOCAL"     ] || LOCAL=localhost  # localhost for local
[ -n "$REMOTE"    ] || REMOTE=localhost # by default remote is localhost also
[ -n "$IS_REMOTE" ] || IS_REMOTE=       # by default it is not remote
[ -n "$CUSTOMERS" ] || CUSTOMERS=10     # by default 10 customers


# === Script configuration
# ======================================================================

function help {
	echo "Usange:"
	echo "  --port PORT    base port number"
	echo "  --servers N    number of servers"
	echo "  --debug N      leaves server N to be executed in a IDE or - to debug operator"
	echo "  --local ADDR   inet addres/ip for local server"
	echo "  --remote ADDR  inet addres/ip for remote server"
	echo "  --start M      local has M servers to start, +1 to include operator"
	echo "  --is-remote    starts remote servers instead of local"
	echo "  --customers N  number of customers"
	echo "  --killall      WARNING: killall java processes about uoc"
	echo "  --help         shows this help and exits"
	echo "  --             parameters for "
	$CMD --help
}

# if [ "$1" == "" ]; then
# 	help
# 	exit 1
# fi

while [ -n "$1" ]; do
	if [ "$1" == "--" ]; then
		shift
		break;
	fi

	case "$1" in
		--port)
			PORT=$2
			shift 2
			;;
		--servers)
			SERVERS=$2
			shift 2
			;;

		--debug)
			DEBUG=$2
			shift 2
			;;

		--local)
			LOCAL=$2
			shift 2
			;;

		--remote)
			REMOTE=$2
			shift 2
			;;

		--start)
			START=$2
			shift 2
			;;

		--is-remote)
			IS_REMOTE=yes
			shift
			;;

		--customers)
			CUSTOMERS=$2
			shift 2
			;;

		--killall)
			echo -n "Killing javas."
			sleep 1
			echo -n "."
			sleep 1
			echo -n "."
			sleep 1
			echo -n "."
			PS=$(ps xf | grep java | grep edu.uoc.sd | cut -b 1-5)
			kill -15 $PS >& /dev/null
			sleep 1
			echo -n "."
			sleep 1
			kill -9 $PS >& /dev/null
			echo " done."
			exit 0
			;;

		--help)
			help
			exit 0
			;;
		*)
			help
			exit 1
	esac
done


# === Script start experiment!
# ======================================================================

# - refill defaults
# start locals as many servers if not telled
[ -n "$START" ] || START=$SERVERS

# - compute machines
# compute servers
MACHINES=""
CURRENT=1
while [ $CURRENT -le $START ]; do

	MACHINES="$MACHINES --server $CURRENT $LOCAL:$PORT"
	PORT=$((PORT + 1))
	CURRENT=$((CURRENT + 1))
done
while [ $CURRENT -le $SERVERS ]; do

	MACHINES="$MACHINES --server $CURRENT $REMOTE:$PORT"
	PORT=$((PORT + 1))
	CURRENT=$((CURRENT + 1))
done

# compute customers
MACHINES="$MACHINES --customer $REMOTE:$PORT"

# - fill common arguments
ARGS=""
ARGS="$ARGS --servers $SERVERS"
ARGS="$ARGS --customers $CUSTOMERS"
ARGS="$ARGS $MACHINES"


# - start processes

# if debug, ask gracefully to start manually a server
if [ -n "$DEBUG" ]; then
	echo "Debug configuration aruments:"
	echo -n ">> $ARGS --me $DEBUG --operator"
	if [ "$DEBUG" == "$((SERVERS + 1))" -o "$DEBUG" == "-" ]; then
		echo -n " yes"
	else
		echo -n " no"
	fi
	echo " ""$@"

	echo "Please, start your server and press enter"
	read
fi

# start local processes
if [ -z "$IS_REMOTE" ]; then
	ME=1
	while [ $ME -le $START -a $ME -le $SERVERS ]; do
		# do not start process if debug and ME=1
		if [ "$DEBUG" != "$ME" ]; then
			NODE="Sv$ME" cmd $ARGS --me $ME --operator no "$@"
		fi
		ME=$((ME + 1))
	done
fi

# start remote processes if i'm remote
if [ -n "$IS_REMOTE" ]; then
	ME=$((START + 1))
	while [ $ME -le $SERVERS ]; do
		if [ "$DEBUG" != "$ME" ]; then
			NODE="Sv$ME" cmd $ARGS --me $ME --operator no "$@"
		fi
		ME=$((ME + 1))
	done
fi

# start customer process
if [ -n "$IS_REMOTE" -o $SERVERS -eq $START ]; then
	if [ "$DEBUG" != "-" ]; then
		NODE="Cu" cmd $ARGS --me - --operator yes "$@"
	fi
fi

# - wait processes
wait
